﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace ActionModel.WebModel
{
	abstract public class MultiActionAjaxHandler : IHttpHandler
	{
		protected HttpResponse Response;
		protected HttpRequest Request;

		Dictionary<string, Action> _ActionDict = new Dictionary<string, Action>();
		/// <summary>
		/// Action 调用方法目录
		/// </summary>
		protected Dictionary<string, Action> ActionDict
		{
			get { return _ActionDict; }
			//set { _ActionDict = value; }
		}

		public void ProcessRequest(HttpContext context)
		{
			Response = context.Response;
			Request = context.Request;

			DoProcessRequest(context);

			RegisterAction();

			if (Request.HttpMethod == "GET")
			{

			}
			else if (Request.HttpMethod == "POST")
			{
				string requestAction = Request.QueryString["action"];
				if (!string.IsNullOrEmpty(requestAction))
				{
					if (_ActionDict.ContainsKey(requestAction))
					{
						Action action = _ActionDict[requestAction];
						action();
					}
				}
			}
		}

		abstract protected void RegisterAction();

		abstract protected void DoProcessRequest(HttpContext context);

		public abstract bool IsReusable
		{
			get;
		}
	}
}
